package com.apps.gomdiem.fragment;

import java.util.ArrayList;

import com.apps.gomdiem.R;
import com.apps.gomdiem.OnDataPass;
import com.apps.gomdiem.activity.MainActivity;
import com.apps.gomdiem.adapter.GridAdapter;
import com.apps.gomdiem.adapter.GridAdapter.ViewHolder;
import com.apps.gomdiem.helper.DataBaseHelper;
import com.apps.gomdiem.helper.HomeAsyncTask;
import com.apps.gomdiem.model.ListenerClass;
import com.apps.gomdiem.model.MainModel;
import com.apps.gomdiem.model.Store;
import com.apps.gomdiem.view.HorizontalListView1;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class HomeTabFragment extends Fragment implements OnClickListener{
	private GridAdapter adapter;
	private String[] myChoice;
	private static GridAdapter filterAdapter;
	private GridView grid;
	OnDataPass mDataPass;
	private HorizontalListView1 listHorizon;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.home_activity, container, false);
		DataBaseHelper db = new DataBaseHelper(getActivity());
		ArrayList<Store> listAllStore = db.getAllStore();
		// Gridview setup
		try {
			grid = (GridView) root.findViewById(R.id.gridView1);
			adapter = new GridAdapter(listAllStore, getActivity());
			grid.setAdapter(adapter);

			grid.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> pAdapter, View pView,
						int pPosition, long pId) {

					ViewHolder view = (ViewHolder) pView.getTag();
					String idStore = view.getIdStore();
					Toast.makeText(getActivity(), "lay id " + idStore,
							Toast.LENGTH_SHORT).show();
					// mDataPass.onDataPass(idStore);
					String a = ((MainModel) MainModel.getInstant())
							.setIdStore(idStore);
					MainActivity.mTabHost.setCurrentTab(2);

				}

			});
		} catch (Exception e) {
		}

		listHorizon = (HorizontalListView1) root.findViewById(R.id.list_horizon);
		
		Button btnfillter1 = (Button) root.findViewById(R.id.btnfillter_1);
		btnfillter1.setOnClickListener(this);
		Button btnfillter2 = (Button) root.findViewById(R.id.btnfillter_2);
		btnfillter2.setOnClickListener(this);
		Button btnfillter3 = (Button) root.findViewById(R.id.btnfillter_3);
		btnfillter3.setOnClickListener(this);
		
//		// Spinner setup
//		myChoice = this.getResources().getStringArray(R.array.array_listChoice);
//		Spinner spinner = (Spinner) root.findViewById(R.id.spinner_choice);
//		// Create an ArrayAdapter using the string array and a default spinner
//		// layout
//		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
//				getActivity(), R.array.array_listChoice,
//				android.R.layout.simple_spinner_item);
//
//		// Specify the layout to use when the list of choices appears
//		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//		// Apply the adapter to the spinner
//		spinner.setAdapter(adapter);
//		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
//
//			@Override
//			public void onItemSelected(AdapterView<?> pAdapter, View pView,
//					int pPosition, long pId) {
//				// ChoiceListener(pPosition);
//				Log.i("position", "=" + pPosition);
//				HomeAsyncTask pHome = new HomeAsyncTask(new ListenerClass() {
//
//					@Override
//					public void doArraylistStoreHorizonal(
//							ArrayList<Store> arrayList) {
//						// filterStore=arrayList;
//
//						filterAdapter = new GridAdapter(arrayList,
//								getActivity());
//						listHorizon.setAdapter(filterAdapter);
//					}
//				});
//				pHome.execute(pPosition);
//			}
//
//			@Override
//			public void onNothingSelected(AdapterView<?> arg0) {
//
//			}
//		});

		// // ListView setup

		return root;
	}
	
	
	private void getAdapter(Integer pPosition){
		HomeAsyncTask pHome = new HomeAsyncTask(new ListenerClass() {

			@Override
			public void doArraylistStoreHorizonal(
					ArrayList<Store> arrayList) {
				// filterStore=arrayList;

				filterAdapter = new GridAdapter(arrayList,
						getActivity());
				listHorizon.setAdapter(filterAdapter);
			}
		});
		pHome.execute(pPosition);
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnfillter_1:
			getAdapter(0);
			break;
		case R.id.btnfillter_2:
			getAdapter(1);
			break;
		case R.id.btnfillter_3:
			getAdapter(2);
			break;

		default:
			break;
		}
	}

}
